﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace VisionCore
{
    public class Status_SortST : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;
        bool SortMapLogSaved = false;

        SystemTimeCounter scStatus = new SystemTimeCounter();
        public void Main()
        {
            condition = Kernel.motion.ExcTR_X.IsHomeAlready && Kernel.motion.ExcTR_R.IsHomeAlready && Kernel.motion.ExcTR_Z.IsHomeAlready
                     && Kernel.motion.SortST_X.IsHomeAlready && Kernel.motion.SortST_Y.IsHomeAlready && Kernel.motion.SortST_R.IsHomeAlready;
            start = condition && AutoMode;

            switch (SortSTStatusCode)
            {
                case SortSTStatus.Init:
                    subStatusCode = "00: 准备";
                    {
                        if (start)
                        {
                            //if(SortST_Presence && !ParameterHelper.SortST.SortCompletFlag)
                            //{
                            //    SortSTStatusCode = SortSTStatus.XY_ToWait;
                            //}
                            //else if (SortST_Presence && ParameterHelper.SortST.SortCompletFlag)
                            //{
                            //    SortSTStatusCode = SortSTStatus.UnloadToEfem;
                            //}
                            //else if (SortST_Empty)
                            //{
                            //    if (SortST_X_ULD)
                            //    {
                            //        SortSTStatusCode = SortSTStatus.CheckStatus;
                            //    }
                            //    else
                            //    {
                            //        SortSTStatusCode = SortSTStatus.XY_ToWait;
                            //    }
                                
                            //}
                        }
                    }
                    break;
                case SortSTStatus.XY_ToWait:
                    subStatusCode = "01: XY移动到安全位";
                    {
                        if (SortST_X_Wait && SortST_Y_Wait)
                        {
                            if (!ExcTR_FlipReq)
                            {
                                SortSTStatusCode = SortSTStatus.CheckStatus;
                            }
                            else
                            {
                                SortSTStatusCode = SortSTStatus.XY_ToUnloadforTRFlip;
                            }
                        }

                    }
                    break;
                case SortSTStatus.XY_ToUnloadforTRFlip:
                    subStatusCode = "02: XY移动到载出位避让for倒膜移载翻转";
                    {
                        if (SortST_X_ULD && SortST_Y_ULD && !ExcTR_FlipReq)
                        {
                            SortSTStatusCode = SortSTStatus.CheckStatus;
                        }
                    }
                    break;
                case SortSTStatus.CheckStatus:
                    subStatusCode = "05: 状态判断";
                    {
                        if (/*SortST_Presence*/true)
                        {
                            if (SortPass)
                            {
                                SortSTStatusCode = SortSTStatus.UnloadToEfem;//下料（UnloadToEfem）
                            }
                            else
                            {
                                if(!LaserIDAutoGetPass && !ParameterHelper.SortST.laserIniInfo.IniFileReadFlag)
                                {
                                    SortSTStatusCode = SortSTStatus.IniFileRead;//INI文件读取
                                }
                                else if((LaserIDAutoGetPass || (!LaserIDAutoGetPass && ParameterHelper.SortST.laserIniInfo.IniFileReadFlag))&&(!SortFrameFlatnessComplete&&!SortSTFrameCheckPass))
                                {
                                    SortSTStatusCode = SortSTStatus.FrameFlatnesCheckReq;//Frame平整度检查
                                }
                                else if ((SortFrameFlatnessComplete|| SortSTFrameCheckPass) && (!SortSTFirstAlignOK && !SortSTPreAlignPass) &&VisionAutoMode)
                                {
                                    RTCHelper.DIO.L_SortSTWaferID = ParameterHelper.SortST.Wafer_ID;
                                    //RTCHelper.DIO.L_SortSTWaferExcInspStartTime = Convert.ToString(ParameterHelper.SortST.ExcInspStartDT);
                                    SortSTStatusCode = SortSTStatus.PreAlignReq;//初对位请求
                                }
                                else if ((SortFrameFlatnessComplete || SortSTFrameCheckPass) && (SortSTFirstAlignOK||SortSTPreAlignPass))
                                {
                                    RTCHelper.DIO.L_SortSTWaferID = ParameterHelper.SortST.Wafer_ID;
                                    //RTCHelper.DIO.L_SortSTWaferExcInspStartTime = Convert.ToString(ParameterHelper.SortST.ExcInspStartDT);
                                    SortSTStatusCode = SortSTStatus.Sorter;//挑拣
                                }
                            }
                        }
                        else if (/*SortST_Empty*/ true)
                        {
                            if (ParameterHelper.CurParam.Mode.LoadMode == "倒膜平台")
                            {
                                SortSTStatusCode = SortSTStatus.LoadFromExcTR;//Load From ExcTR
                            }
                            else if (ParameterHelper.CurParam.Mode.LoadMode == "挑拣平台")
                            {
                                SortSTStatusCode = SortSTStatus.LoadFromEfem;//Load From Efem
                            }
                        }
                    }
                    break;
                # region Load From ExcTR
                case SortSTStatus.LoadFromExcTR: //Load From ExcTR
                    subStatusCode = "10: FromExcTR入料";
                    {
                       
                            SortSTStatusCode = SortSTStatus.PinCylinder_ToDown_1st;
                       
                    }
                    break;
                case SortSTStatus.PinCylinder_ToDown_1st:
                    subStatusCode = "11: 顶Pin气缸下降";
                    {
                        if (SortST_Down)
                        {
                            SortSTStatusCode = SortSTStatus.XY_ToUnload;
                        }
                    }
                    break;
                case SortSTStatus.XY_ToUnload:
                    subStatusCode = "12: XY移动到出料位";
                    {
                            SortSTStatusCode = SortSTStatus.XR_ToLoad;
                     
                    }
                    break;
                case SortSTStatus.XR_ToLoad:
                    subStatusCode = "13: XR移动到FromExcTR入料位";
                    {
                        if (SortST_X_LD && SortST_R_Transfer)
                        {
                            SortSTStatusCode = SortSTStatus.Y_ToLoad;
                        }
                    }
                    break;
                case SortSTStatus.Y_ToLoad:
                    subStatusCode = "14: Y移动到FromExcTR入料位";
                    {
                        if (SortST_Y_LD)
                        {
                            SortSTStatusCode = SortSTStatus.LoadFromExcTRReq;
                        }
                    }
                    break;
                case SortSTStatus.LoadFromExcTRReq:
                    subStatusCode = "20: FromExcTR入料请求";
                    {
                        if (ExcTRStatusCode == ExcTRStatus.Z_ToUnload && ExcTR_R_LD && ExcTR_Z_ULD)
                        {
                            SortSTStatusCode = SortSTStatus.ST_VacOn_1st;
                        }
                    }
                    break;
                case SortSTStatus.ST_VacOn_1st:
                    subStatusCode = "21: 真空On";
                    {
                        if (ExcTRStatusCode == ExcTRStatus.Z_ToFinUnload && (SortST_Vac||DryRunMode) && (ExcTR_Open||true) && ExcTR_Z_FinULD)
                        {
                            ParameterHelper.SortSTDAL.Copy(ParameterHelper.FileName_ExcTR);
                            ParameterHelper.SortST = ParameterHelper.SortSTDAL.Load();
                            ParameterHelper.ExcTR = new ChipInfo();
                            SaveExcTR();
                            if (DryRunMode)
                            {
                                SortFrameFlatnessComplete = true;
                                SortSTFirstAlignOK = true;
                            }
                            else
                            {
                                SortFrameFlatnessComplete = false;
                                SortSTFirstAlignOK = false;
                                Laser_ToPowerCheck_A = true;
                            }

                            SortSTStatusCode = SortSTStatus.Y_ToUnload_2nd;

                        }
                    }
                    break;
                case SortSTStatus.Y_ToUnload_2nd:
                    subStatusCode = "22: Y移动到UnLoad位";
                    {
                        if (SortST_Y_ULD)
                        {
                            SortSTStatusCode = SortSTStatus.LoadFromExcTRComplete;
                        }
                    }
                    break;
                case SortSTStatus.LoadFromExcTRComplete:
                    subStatusCode = "23: FromExcTR入料完成";
                    {
                        if (ExcTR_Z_Wait)
                        {
                            SortSTStatusCode = SortSTStatus.XY_ToWait;
                        }
                    }
                    break;
                #endregion

                #region Load From Efem
                case SortSTStatus.LoadFromEfem://Load From Efem
                    subStatusCode = "30: FromEfem入料";
                    {
                            SortSTStatusCode = SortSTStatus.PinCylinder_ToDown_2nd;                        
                    }
                    break;
                case SortSTStatus.PinCylinder_ToDown_2nd:
                    subStatusCode = "31: 顶Pin气缸下降";
                    {
                        if (SortST_Down)
                        {
                            SortSTStatusCode = SortSTStatus.XYR_ToUnload_1st;
                        }
                    }
                    break;
                case SortSTStatus.XYR_ToUnload_1st:
                    subStatusCode = "32: XYR移动到FromEfem入料位";
                    {
                        if (SortST_X_ULD && SortST_Y_ULD && SortST_R_Transfer && SortST_X_AtULD)
                        {
                            SortSTStatusCode = SortSTStatus.PinCylinder_ToUp_1st;
                        }
                    }
                    break;
                case SortSTStatus.PinCylinder_ToUp_1st:
                    subStatusCode = "33: 顶pin气缸上升";
                    {
                        if (SortST_Up)
                        {
                            SortSTStatusCode = SortSTStatus.LoadFromEfemReq;
                        }
                    }
                    break;


                case SortSTStatus.LoadFromEfemReq:
                    subStatusCode = "40: FromEfem入料请求";
                    {
                       
                            SortSTStatusCode = SortSTStatus.WaitingLoad;
                        
                    }
                    break;
                case SortSTStatus.WaitingLoad:
                    subStatusCode = "41: 等待FromEfem入料";
                    {
                        if (ParameterHelper.SortST.Enable)
                        {
                            SortSTStatusCode = SortSTStatus.LoadFromEfemComplete;
                            scStatus.Start();

                        }
                    }
                    break;
                case SortSTStatus.LoadFromEfemComplete:
                    subStatusCode = "42: FromEfem入料完成";
                    {

                        if (ParameterHelper.SortST.Enable)
                        {
                            SortSTStatusCode = SortSTStatus.PinCylinder_ToDown_3rd;
                        }
                    }
                    break;
                case SortSTStatus.PinCylinder_ToDown_3rd:
                    subStatusCode = "43: 顶pin气缸下降";
                    {
                        if (SortST_Down)
                        {
                            SortSTStatusCode = SortSTStatus.ST_VacOn_2nd;
                        }
                    }
                    break;
                case SortSTStatus.ST_VacOn_2nd:
                    subStatusCode = "44: 真空ON";
                    {
                       
                            SortSTStatusCode = SortSTStatus.XY_ToWait;
                      
                    }
                    break;
                #endregion

                #region InI文件读取
                case SortSTStatus.IniFileRead://INI恩建读取
                    subStatusCode = "50: InI文件读取";

                   
                    if (!LaserIDAutoGetPass)//20230202liang新增
                    {
                        laserIniPath = "C:\\Download" + "\\" + ParameterHelper.SortST.Wafer_ID + ".ini";
                        if (File.Exists(laserIniPath))//20230202liang新增
                        {
                            //ParameterHelper.SortST.laserIniInfo.IniContent.X = Convert.ToDouble(filesINI.INIRead("Content", "X", laserIniPath));
                            //ParameterHelper.SortST.laserIniInfo.IniContent.Y = Convert.ToDouble(filesINI.INIRead("Content", "Y", laserIniPath));
                            //ParameterHelper.SortST.laserIniInfo.IniContent.Line1 = filesINI.INIRead("Content", "LINE1", laserIniPath);
                            //ParameterHelper.SortST.laserIniInfo.IniContent.Line2 = filesINI.INIRead("Content", "LINE2", laserIniPath);
                            //ParameterHelper.SortST.laserIniInfo.IniContent.Line3 = filesINI.INIRead("Content", "LINE3", laserIniPath);
                            //ParameterHelper.SortST.laserIniInfo.IniContent.Line4 = filesINI.INIRead("Content", "LINE4", laserIniPath);
                        }
                        if (!LaserIDAutoGetPass && File.Exists(laserIniPath) && ParameterHelper.SortST.laserIniInfo.IniContent.Line1.Length <= ParameterHelper.CurParam.LaserParam.LaserBlockColNum && ParameterHelper.SortST.laserIniInfo.IniContent.Line2.Length <= ParameterHelper.CurParam.LaserParam.LaserBlockColNum && ParameterHelper.SortST.laserIniInfo.IniContent.Line3.Length <= ParameterHelper.CurParam.LaserParam.LaserBlockColNum && ParameterHelper.SortST.laserIniInfo.IniContent.Line4.Length <= ParameterHelper.CurParam.LaserParam.LaserBlockColNum && (ParameterHelper.SortST.laserIniInfo.IniContent.Line1.Length >0|| ParameterHelper.SortST.laserIniInfo.IniContent.Line2.Length > 0|| ParameterHelper.SortST.laserIniInfo.IniContent.Line3.Length > 0||ParameterHelper.SortST.laserIniInfo.IniContent.Line4.Length > 0))//20230202liang新增
                        {
                            ParameterHelper.SortST.laserIniInfo.IniFileReadFlag = true;
                            SaveSortST();
                            SortSTStatusCode = SortSTStatus.XY_ToWait;
                        }
                    }

                    break;
                #endregion

                #region Frame平整度检查
                case SortSTStatus.FrameFlatnesCheckReq://Frame平整度检查
                    subStatusCode = "60: FrameFlatnesCheck请求";
                    {
                        if (SortFrameFlatnessComplete)
                        {
                            SortSTStatusCode = SortSTStatus.FrameFlatnesChekComplete;
                        }
                    }
                    break;
                case SortSTStatus.FrameFlatnesChekComplete:
                    subStatusCode = "61: FrameFlatnesCheck完成";
                    {
                        if (SortFrameFlatnessComplete)
                        {
                            SortSTStatusCode = SortSTStatus.XY_ToWait;
                        }
                    }
                    break;
                #endregion

                #region 初对位
                case SortSTStatus.PreAlignReq:
                    subStatusCode = "70: 初对位请求";
                    {
                        RTCHelper.IOEvent.HSG_SortSTFirstAlignmentTrigger.Trig();
                        SortSTFirstAlignOK = false;
                        //ParameterHelper.SortST.ReadMapFlag = false;
                        SaveSortST();
                        SortSTStatusCode = SortSTStatus.PreAlignComplete;                       

                    }
                    break;
                case SortSTStatus.PreAlignComplete:
                    subStatusCode = "71: 初对位完成";
                    {
                        if (SortSTFirstAlignOK)
                        {
                            SortSTStatusCode = SortSTStatus.CheckStatus;
                        }
                    }
                    break;
                #endregion

                #region 挑拣
                case SortSTStatus.Sorter://进入挑选
                    subStatusCode = "80: 进入挑选";
                    {
                        if (SortStatusCode == SortStatus.Init)
                        {
                            SortSTStatusCode = SortSTStatus.SorterComplete;
                        }
                        else if(SortStatusCode == SortStatus.Init && ExcTR_FlipReq)
                        {
                            SortSTStatusCode = SortSTStatus.XY_ToWait;
                        }
                    }
                    break;
                case SortSTStatus.SorterComplete:
                    subStatusCode = "81: 挑选完成";
                    {
                     
                    }
                    break;
                #endregion

                #region UnloadToEfem
                case SortSTStatus.UnloadToEfem:
                    subStatusCode = "90: 出料至Efem";
                    {
                        SortSTStatusCode = SortSTStatus.XYR_ToUnload_2nd;

                    }
                    break;
                case SortSTStatus.XYR_ToUnload_2nd:
                    subStatusCode = "91: XYR移动到出料位";
                    {
                        if (SortST_X_ULD && SortST_Y_ULD && SortST_R_Transfer && SortST_X_AtULD)
                        {
                            SortSTStatusCode = SortSTStatus.ST_VacOff;
                        }
                    }
                    break;
                case SortSTStatus.ST_VacOff:
                    subStatusCode = "92: 真空OFF";
                    {
                        if (!SortST_Vac)
                        {
                            SortSTStatusCode = SortSTStatus.PinCylinder_ToUp_2nd;
                        }
                    }
                    break;
                case SortSTStatus.PinCylinder_ToUp_2nd:
                    subStatusCode = "93: 顶pin气缸上升";
                    {
                        if (SortST_Up)
                        {
                            SortMapLogSaved = false;
                            ParameterHelper.SortST.UnloadFlag = true;
                            SaveSortST();
                            SortSTStatusCode = SortSTStatus.UnloadReq;
                        }
                    }
                    break;
                case SortSTStatus.UnloadReq:
                    subStatusCode = "95: 出料请求";
                    {
                        if (ParameterHelper.SortST.Wafer_ID!="" && !SortMapLogSaved)//将Map图存档
                        {
                            string ID = ParameterHelper.SortST.Wafer_ID;
                            string filePath = string.Format(@"E:\Log\ProductData\SortSTMap\{0}", DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss "));
                            string filedest = string.Format(@"{0}\{1}", filePath, ID.ToString());
                            string file = Environment.CurrentDirectory + string.Format(@"\WaferResult_ToMotion\{0}", ID.ToString());
                            if (Directory.Exists(filePath) == false)
                            {
                                Directory.CreateDirectory(filePath);
                            }
                            if (File.Exists(file))
                            {
                                File.Copy(file, filedest);
                                File.Delete(file);
                            }
                            SortMapLogSaved = true;
                        }
                        

                        if (!DryRunMode)
                        {
                          
                                SortSTStatusCode = SortSTStatus.RBToSortUnload;
                           
                        }
                        else
                        {
                            ParameterHelper.SortST = new ChipInfo();
                            SaveSortST();
                            SortSTStatusCode = SortSTStatus.PinCylinder_ToDown_4th;
                        }
                    }
                    break;
                case SortSTStatus.RBToSortUnload:
                    subStatusCode = "96: Robot到SortUnload取料";
                    {

                            SortSTStatusCode = SortSTStatus.PinCylinder_ToDown_4th;
                    }
                    break;
                case SortSTStatus.PinCylinder_ToDown_4th:
                    subStatusCode = "97: 顶Pin气缸下降";
                    {
                        if (SortST_Down)
                        {
                            SortSTStatusCode = SortSTStatus.UnloadComplete;
                            scStatus.Start();

                        }
                    }
                    break;
                case SortSTStatus.UnloadComplete:
                    subStatusCode = "98: 出料完成";
                    {
                            SortSTStatusCode = SortSTStatus.Init;
                        
                    }
                    break;
                #endregion

                default:
                    break;
            }

            LogStatusCode(subStatusCode);
        }

        private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("挑拣平台- " + tempStatusCode);
            }
        }
    }
}
